package com.tengju.bff.interfaces.covercharge.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.tengju.bff.interfaces.covercharge.constant.OnlinePayMoneyErrorNum;
import com.tengju.bff.interfaces.covercharge.constant.OnlinePayMoneyTypeEnum;
import com.tengju.bff.interfaces.covercharge.constant.ServiceWithdarwCycleAddTransOrderEnum;
import com.tengju.bff.interfaces.covercharge.entity.dto.CycleAddInfo;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceWithdarwCycleAddTransOrder;
import com.tengju.bff.interfaces.covercharge.service.IServiceWithdarwCycleAddService;
import com.tengju.bff.interfaces.covercharge.service.IServiceWithdarwCycleAddTransOrderService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author cj
 * @Classname OnlinePayMoneyJob
 * @Description 代发重试定时任务
 * @Date 2020/10/29
 */
//乐税打款结果
@Slf4j
@Component
public class OnlinePayMoneyJob {

    @Autowired
    private IServiceWithdarwCycleAddTransOrderService serviceWithdarwCycleAddTransOrderService;

    @Autowired
    private IServiceWithdarwCycleAddService serviceWithdarwCycleAddService;

    //每页数量
    private int pageSize = 1000;

    @XxlJob("onlinePayMoneyJob")
    public ReturnT<String> execute(String s){
        log.info("[服务费后台][代发补偿][onlinePayMoneyJob定时任务开始执行]");
        try {
            List<Integer> statusList = Lists.newArrayList();
            statusList.add(ServiceWithdarwCycleAddTransOrderEnum.DEALING.getCode());
            statusList.add(ServiceWithdarwCycleAddTransOrderEnum.UNKNOWN.getCode());
            QueryWrapper<ServiceWithdarwCycleAddTransOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("status",statusList);
            queryWrapper.lt("num", OnlinePayMoneyErrorNum.SERVICE_WITHDARE_CYCLE_ADD_TRANS);
            int count = serviceWithdarwCycleAddTransOrderService.count(queryWrapper);
            if (count == 0) {
                return ReturnT.SUCCESS;
            }

            //循环遍历每页
            for (int i = 1; i <= getPage(count); i++) {
                queryWrapper.orderByAsc("created_time");
                Page<ServiceWithdarwCycleAddTransOrder> pageResult = serviceWithdarwCycleAddTransOrderService.getPageListByQuery(new Page<>(i, pageSize), queryWrapper);
                List<ServiceWithdarwCycleAddTransOrder> list = pageResult.getRecords();
                log.info("[服务费后台][代发补偿][onlinePayMoneyJob定时任务开始执行]，page={},pageSize={},result={}", i , pageSize, list.stream().map(e -> e.getId()).collect(Collectors.toList()));
                if (CollectionUtils.isEmpty(list)) {
                    return ReturnT.SUCCESS;
                }
                for (ServiceWithdarwCycleAddTransOrder cycleAddTransOrder : list) {
                    //请求数据组装
                    CycleAddInfo cycleAddInfo=new CycleAddInfo();
                    cycleAddInfo.setApprovers(cycleAddTransOrder.getWithdrawer());
                    cycleAddInfo.setCycleAddId(cycleAddTransOrder.getCycleAddId());
                    cycleAddInfo.setBizId(String.valueOf(IdWorker.getId()));
                    cycleAddInfo.setType(OnlinePayMoneyTypeEnum.QUERY.getCode());
                    //mq处理
                    //serviceWithdarwCycleAddService.sendOnlinePayMoneyMessage(cycleAddInfo);
                    serviceWithdarwCycleAddService.dealOnlinePayMoney(cycleAddInfo);
                }
            }
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.error("[服务费后台][代发补偿][onlinePayMoneyJob定时任务执行过程中有异常抛出]", e);
            return ReturnT.FAIL;
        }
    }

    private int getPage(long count) {
        return Math.toIntExact((count / pageSize + (count % pageSize > 0 ? 1 : 0)));
    }
}
